# -*- coding: utf-8 -*-

import gdata.urlfetch
import gdata.service
import gdata.youtube
import gdata.youtube.service
import gdata.alt.appengine

class YouTube():
  def LoadVideos(self, ytquery, ytorderby, ythowmany):
    client = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()
    client.developer_key = 'AI39si52WXrBshWywE-dGkiHvTzH35eWcTEl9aM98YaNotIrVjvmuo89eE6NjUKmJcs1-duD5lKEWimgVp7v5paSHLwGggegSw'
    
    query.vq = ytquery.encode('utf-8')
    query.orderby = ytorderby
    query.max_results = ythowmany

    feed = client.YouTubeQuery(query)

    titles = []
    thumbnails = []
    descriptions = []
    categories = []
    urlvideos = []
    times = []
    dates = []
    tags = []
    ids = []
    videoids = []
    authors = []

    for entry in feed.entry:
      titles.append(entry.title.text)
      thumbnails.append(entry.media.thumbnail[0].url) 
      descriptions.append(entry.media.description)
      categories.append(entry.media.category[0]) 
      urlvideos.append(entry.media.player.url)
      dates.append(entry.published.text.split('T')[0]) 
      times.append(entry.published.text.split('T')[1][:5])
      tags.append(entry.media.keywords)
      ids.append(entry.id.text)
      videoids.append(entry.id.text.split('/')[5])
      authors.append(entry.author[0].name.text)

    videos = zip(titles, thumbnails, descriptions, categories, urlvideos, times, dates, tags, ids, videoids, authors)
    
    if len(videos) > 0:
      return videos
    else:
      videos = [query.vq]
      return videos

  def LoadVideosUser(self, ytuser, ytquery, ytorderby, ythowmany):
    client = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()
    client.developer_key = 'AI39si52WXrBshWywE-dGkiHvTzH35eWcTEl9aM98YaNotIrVjvmuo89eE6NjUKmJcs1-duD5lKEWimgVp7v5paSHLwGggegSw'

    query.vq = ytquery.encode('utf-8')
    query.orderby = ytorderby
    query.max_results = ythowmany
    query.author = ytuser

    feed = client.YouTubeQuery(query)

    titles = []
    thumbnails = []
    descriptions = []
    categories = []
    urlvideos = []
    times = []
    dates = []
    tags = []
    ids = []
    videoids = []
    authors = []

    for entry in feed.entry:
      titles.append(entry.title.text)
      thumbnails.append(entry.media.thumbnail[0].url) 
      descriptions.append(entry.media.description)
      categories.append(entry.media.category[0]) 
      urlvideos.append(entry.media.player.url)
      dates.append(entry.published.text.split('T')[0]) 
      times.append(entry.published.text.split('T')[1][:5])
      tags.append(entry.media.keywords)
      ids.append(entry.id.text)
      videoids.append(entry.id.text.split('/')[5])
      authors.append(entry.author[0].name.text)

    videos = zip(titles, thumbnails, descriptions, categories, urlvideos, times, dates, tags, ids, videoids, authors)

    if len(videos) > 0:
      return videos
    else:
      videos = [query.vq]
      return videos


  def LoadVideo(self, ytvideoid):
     client = gdata.youtube.service.YouTubeService()
     query = gdata.youtube.service.YouTubeVideoQuery()
     client.developer_key = 'AI39si52WXrBshWywE-dGkiHvTzH35eWcTEl9aM98YaNotIrVjvmuo89eE6NjUKmJcs1-duD5lKEWimgVp7v5paSHLwGggegSw'
     
     entry = client.GetYouTubeVideoEntry(video_id=ytvideoid)
     
     return entry